package in.muton.edushare.grading.panels.studentGrades;

import in.muton.edushare.grading.panels.prevgrades.old.StudentGradeEdit;
import in.muton.edushare.grading.panels.prevgrades.old.StudentSemGradeView;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.pages.curricula.CurriculaSummary;
import in.muton.edushare.wicket.models.CayenneList;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;

import com.edushare.common.dao.BatchDAO;
import com.edushare.common.vo.Batches;
import com.edushare.common.vo.Users;

public class StudentProgramGradePanel extends EdusharePanel {
	private static final long serialVersionUID = 296346325006896775L;
	private IModel<Users> userModel = null;
	private CayenneList<Batches> batchList = new CayenneList<Batches>(null);
	private IModel<Batches> activeBatch = null;

	public StudentProgramGradePanel(String id, IModel<Users> model,
			EduShareBasePage page) {
		super(id, model, page);
		this.userModel = model;
		batchList = new CayenneList<Batches>(new BatchDAO(getORMInterface())
				.getUserBatches(userModel.getObject()));
		if (batchList.getObject().size() > 0) {
			setActiveBatch(batchList.getObject().get(0));
		}
		init();
	}

	private void init() {
		add(new ListView<IModel<Batches>>("progNameLV", batchList) {
			@Override
			protected void populateItem(final ListItem<IModel<Batches>> arg0) {
				Link<Void> l = new Link<Void>("batchSel") {
					@Override
					public void onClick() {
						if (batchList.getObject().size() < 2) {
							PageParameters params = new PageParameters();
							Batches b = arg0.getModelObject().getObject();
							params.add("0", b.getCurricula().getCurriculaId()
									.toString());
							setResponsePage(new CurriculaSummary(params));
						} else
							setActiveBatch(arg0.getModelObject());
					}

					private static final long serialVersionUID = 2816236609972806642L;
				};
				arg0.add(l);
				l.add(new Label("batchName", arg0.getModelObject().getObject()
						.getBatchName()));
			}

			private static final long serialVersionUID = -6765563648000214478L;
		});
		add(new Label("batchName", getBatchName()));

		add(new ListView<Integer>("semesterPanelsLV", new SemArray()) {
			@Override
			protected void populateItem(ListItem<Integer> arg0) {
				arg0
						.add(new StudentSemGradeView("semesterPanel",
								userModel, getActiveBatch(), arg0
										.getModelObject(), getBasePage()));
			}

			private static final long serialVersionUID = -3995510746248570232L;
		});
	}

	public String getBatchName() {
		return getActiveBatch().getObject().getBatchName();
	}

	public void setActiveBatch(IModel<Batches> activeBatch) {
		this.activeBatch = activeBatch;
	}

	public IModel<Batches> getActiveBatch() {
		return activeBatch;
	}

	private class SemArray implements IModel<List<Integer>> {
		public List<Integer> getObject() {
			if (getActiveBatch() == null)
				return new ArrayList<Integer>();
			return StudentGradeEdit.makeList(0, getActiveBatch().getObject()
					.getCurrentSemester());
		}

		public void setObject(List<Integer> arg0) {
		}

		public void detach() {
		}

		private static final long serialVersionUID = -1971861351264507211L;
	}

}
